package com.hs.mod_archive.helper

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.hs.mod_archive.R
import com.hs.mod_archive.databinding.ItemArchiveLogBinding
import com.hs.mod_archive.view.MyListView
import com.hs.room.model.archive.AlarmLog


class AlarmManagerHelper(
    footPageListView: MyListView,
    var onItemClick: OnItemClickListener?,
    footSearchCallback: FootSearchCallback?
) :
    ListViewHelper(footPageListView, footSearchCallback) {
    private val mContext: Context = footPageListView.context
    var stateParent: AlarmLog? = null
    private val viewHolders = arrayOfNulls<ItemArchiveLogBinding>(
        perPageCount
    )
    private val mParentView: LinearLayout = footPageListView.mViewList

    var onClickListener: View.OnClickListener = View.OnClickListener { v ->
        val tag = v.tag
        if (tag is Int && null != onItemClick) {
            val position = tag
            val entity = mEntityList!![position]
            when (v.id) {
                R.id.bt_detail -> onItemClick!!.onItemClick(entity, position)
//                R.id.bt_del -> onItemClick!!.onItemClickDelete(entity, position)
            }
        }
    }

    interface OnItemClickListener {
        fun onItemClick(entity: AlarmLog?, position: Int)

//        fun onItemClickDelete(entity: AlarmLog?, position: Int)
    }

    private var mEntityList: List<AlarmLog>? = null


    fun setListDataAndRefresh(entityList: List<AlarmLog>?) {
        mEntityList = entityList
        if (null == viewHolders[0]) {
            for (i in 0 until perPageCount) {
                R.layout.item_archive_log
                val binding =
                    ItemArchiveLogBinding.inflate(LayoutInflater.from(mContext), mParentView, true)
                viewHolders[i] = binding
                if (isEvenOrOdd(i)) {
                    binding.root.setBackgroundColor(mColorBgOddNumberLine)
                }
                binding.btDetail.setOnClickListener(onClickListener)
            }
        }
        if (null != mEntityList && !mEntityList!!.isEmpty()) {
            mParentView.visibility = View.VISIBLE
            val newListSize = mEntityList!!.size
            for (i in 0 until perPageCount) {
                if (i >= newListSize) {
                    val recordBinding = viewHolders[i]
                    recordBinding!!.root.visibility = View.INVISIBLE
                } else {
                    val holder = viewHolders[i]
                    holder!!.root.visibility = View.VISIBLE
                    stateParent = mEntityList!![i]
                    // 将数据和控件绑定
//                    holder.btEdit.tag = i
//                    holder.btDel.tag = i
//                    holder.tvName.text = stateParent!!.alName
//                    holder.tvRemark.text = stateParent!!.alRemarks
//                    //                    holder.tvTime.setText(stateParent.getDateTime());
//                    if (stateParent!!.enable) {
//                        holder.tvIsEnable.text = enableTrue
//                    } else {
//                        holder.tvIsEnable.text = enableFalse
//                    }
                }
            }
        } else {
            mParentView.visibility = View.INVISIBLE
            for (binding in viewHolders) {
                binding!!.root.visibility = View.INVISIBLE
            }
        }
    }
}
